package Application;

import Application.Controller.*;
import Application.Tool.Pen;
import Application.Tool.Straw;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.Parent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import Application.StatusBarManager.StatusBarManager;


public class Main extends Application {
    // 窗口宽度
    private final int WIDTH = 600;
    // 窗口高度
    private final int HEIGHT = 600;

    private Stage primaryStage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        //showMainWindow();
        showWelcomeWindow();
    }

    public void showMainWindow() {
        try {
            Pen.getInstance().setMain(this);

            FXMLLoader loader =  new FXMLLoader(getClass().getResource("/View/MainWindow.fxml"));
            VBox root = loader.load();

            MainWindowController mainWindowController = loader.getController();
            mainWindowController.setMain(this);
            mainWindowController.setPrimarStage(primaryStage);

            AppJoint.getInstance().loadBlendModeToComboBox(
                    mainWindowController.getCombo()
            );

            AppJoint.getInstance().setMainWindowController(mainWindowController);

            //注入前景色 后景色相关组件
            ColorPicker colorPicker = mainWindowController.getFrontColorPicker();
            Straw.getInstance().setFrontColorPicker(colorPicker);

            Rectangle rectangle = mainWindowController.getFrontColorBox();
            Straw.getInstance().setFrontColorBox(rectangle);

            ColorPicker backColorPicker = mainWindowController.getBackColorPicker();
            Straw.getInstance().setBackColorPicker(backColorPicker);

            Rectangle backRectangle = mainWindowController.getBackColorBox();
            Straw.getInstance().setBackColorBox(backRectangle);

            //设置imageview的高宽

            mainWindowController.getViewport().setFitWidth(AppJoint.getInstance().getCanvasWidth());
            mainWindowController.getViewport().setFitHeight(AppJoint.getInstance().getCanvasHeight());

            // inject StatusBarManager with label from FXML
            StatusBarManager.getInstance().setStatusBar(mainWindowController.getStatusBar());

            AppJoint.getInstance().setImageView(mainWindowController.getImageView());

            //newCanVas初始化
            AppJoint.getInstance().createOutImage();
            AppJoint.getInstance().bondImg();

            Scene scene = new Scene(root,WIDTH, HEIGHT);
            //scene.getStylesheets().add("Application/Assets/Css/test.css");

            setKeyAction(mainWindowController,root);
            setScrollAction(mainWindowController);

            scene.getStylesheets().add("/Application/Assets/css/test.css");
            primaryStage.setTitle("Canvas");
            primaryStage.setScene(scene);
            primaryStage.setMaximized(true);
            primaryStage.show();

            //showNewCanvasWindow();
            //showWelcomeWindow();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showWarnningWindow(String text){
        try{
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/Warnning.fxml"));
            GridPane root = loader.load();

            WarnningController warnningController = loader.getController();
            warnningController.setMain(this);
            warnningController.setText(text);

            Scene scene = new Scene(root,500,150);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.setTitle("错误");
            stage.show();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void showSaveWindow(){
        try{
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/SaveDialogWindow.fxml"));
            GridPane root = loader.load();

            SaveDialogController saveDialogController = loader.getController();
            saveDialogController.setMain(this);


            Scene scene = new Scene(root,500,150);
            Stage stage = new Stage();
            scene.getStylesheets().add("/Application/Assets/css/test.css");
            stage.setScene(scene);
            stage.initModality(Modality.APPLICATION_MODAL);

            saveDialogController.setPrimarStage(primaryStage);
            saveDialogController.setLocalStage(stage);

            stage.setTitle("提示");
            stage.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void showNewCanvasWindow(){
        try{
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/NewCanvasWindow.fxml"));
            Parent root = loader.load();

            NewCanvasController newCanvasController =loader.getController();
            newCanvasController.setMain(this);

            Scene scene = new Scene(root);
            Stage stage = new Stage();
            scene.getStylesheets().add("/Application/Assets/css/newCanvas.css");
            stage.setScene(scene);

            stage.initModality(Modality.APPLICATION_MODAL);

            newCanvasController.setLocalStage(stage);
            newCanvasController.setPrimarStage(primaryStage);

            stage.setTitle("提示");
            stage.show();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void showWelcomeWindow(){
        try{

            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/WelcomeWindow.fxml"));
            Parent root = loader.load();

            WelcomeController welcomeController = loader.getController();
            welcomeController.setMain(this);

            Scene scene = new Scene(root);
            Stage stage = new Stage();
            stage.setScene(scene);
            scene.getStylesheets().add("/Application/Assets/css/welcome.css");
            stage.initModality(Modality.APPLICATION_MODAL);

            welcomeController.setLocalStage(stage);
            welcomeController.setPrimarStage(primaryStage);

            stage.setTitle("欢迎！");

            stage.show();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    //为键盘设置快捷键
    public void setKeyAction(MainWindowController mainWindowController, VBox root){
        root.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                System.out.println(event.getCode());
                if(event.getCode()==KeyCode.UP){
                    mainWindowController.btnMoveUpClicked();
                    return;
                }
                if(event.getCode()==KeyCode.DOWN){
                    mainWindowController.btnMoveDownClicked();
                    return;
                }
                if(event.getCode() == KeyCode.BACK_SPACE){
                    mainWindowController.btnDelLayerClicked();
                    return;
                }
                if(event.getCode() == KeyCode.L){
                    mainWindowController.btnLockOnAction();
                    return;
                }
                if(event.getCode() == KeyCode.R){
                    if(event.isShiftDown()){
                        if(event.isAltDown()){
                            mainWindowController.reverseColorOnAction();
                            return;
                        }
                    }else{
                        mainWindowController.btnRenameClicked();
                        return;
                    }

                }

                if(event.getCode() == KeyCode.S){
                    if(event.isShiftDown()){
                        mainWindowController.savePicOnAction();
                        return;
                    }else{
                        mainWindowController.btnShowClicked();
                        return;
                    }
                }

                if(event.getCode() == KeyCode.N){
                    mainWindowController.btnNewLayerClicked();
                    return;
                }

                if(event.isShiftDown()){
                    if(event.getCode() == KeyCode.T){
                        mainWindowController.newCanvasOnAction();
                        return;
                    }
                    if(event.getCode() == KeyCode.O){
                        mainWindowController.openPic();
                        return;
                    }
                }

                if(event.isAltDown()){
                    if(event.isShiftDown()){
                        if(event.getCode() == KeyCode.EQUALS){
                            mainWindowController.frontColorOnAction();
                            return;
                        }
                        if(event.getCode() == KeyCode.MINUS) {
                            mainWindowController.backColorOnAction();
                            return;
                        }

                        if(event.getCode() == KeyCode.C) {
                            mainWindowController.eraseColorOnAction();
                            return;
                        }
                        if(event.getCode() == KeyCode.G){
                            mainWindowController.greyOnAction();
                            return;
                        }
                    }
                }
            }
        });
    }

    public void setScrollAction(MainWindowController mainWindowController){
        //为滚轮添加事件
        mainWindowController.getGridview().setOnScroll(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent event) {
                if(event.getDeltaY() > 0){
                    mainWindowController.getViewport().setFitHeight(mainWindowController.getViewport().getFitHeight()*1.05);
                    mainWindowController.getViewport().setFitWidth(mainWindowController.getViewport().getFitWidth()*1.05);
                }else{
                    mainWindowController.getViewport().setFitHeight(mainWindowController.getViewport().getFitHeight()*0.905);
                    mainWindowController.getViewport().setFitWidth(mainWindowController.getViewport().getFitWidth()*0.905);
                }
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }

}
